package model;

import javax.xml.transform.Result;

import Iface.WeatherListener;
import Iface.Weatheriface;
import bean.WeatherResult;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import service.Weatherservice;

/**
 * Created by ASUS on 2017/3/14.
 */

public class WeatherModel implements Weatheriface {
    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn/";
    private Weatherservice weatherservice;
    public WeatherModel(){
         retrofit=new Retrofit.Builder().baseUrl(BASEURL)
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();

    }
    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
        weatherservice = retrofit.create(Weatherservice.class);
        Call<WeatherResult> call = weatherservice.getResult(cityno);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
            if (response.isSuccessful()&& response.body()!=null)
                listener.onResponse(response.body().getWeatherinfo());
                else
                listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {

            }
        });
    }

    }
